package com.castle.fortress.admin.exchange.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.exchange.entity.ExchangeConfigEntity;
import com.castle.fortress.admin.exchange.enums.CodeTypeEnum;
import com.castle.fortress.admin.exchange.enums.VoucherStatusEnum;
import com.castle.fortress.admin.exchange.mapper.ExchangeConfigMapper;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.exchange.entity.ExchangeVoucherEntity;
import com.castle.fortress.admin.exchange.dto.ExchangeVoucherDto;
import com.castle.fortress.admin.exchange.mapper.ExchangeVoucherMapper;
import com.castle.fortress.admin.exchange.service.ExchangeVoucherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * 兑换券 服务实现类
 *
 * @author whc
 * @since 2022-12-06
 */
@Service
public class ExchangeVoucherServiceImpl extends ServiceImpl<ExchangeVoucherMapper, ExchangeVoucherEntity> implements ExchangeVoucherService {

    @Autowired
    private ExchangeConfigMapper exchangeConfigMapper;

    /**
     * 获取查询条件
     *
     * @param exchangeVoucherDto
     * @return
     */
    public QueryWrapper<ExchangeVoucherEntity> getWrapper(ExchangeVoucherDto exchangeVoucherDto) {
        QueryWrapper<ExchangeVoucherEntity> wrapper = new QueryWrapper();
        if (exchangeVoucherDto != null) {
            ExchangeVoucherEntity exchangeVoucherEntity = ConvertUtil.transformObj(exchangeVoucherDto, ExchangeVoucherEntity.class);
            wrapper.eq(exchangeVoucherEntity.getExchangeId() != null, "exchange_id", exchangeVoucherEntity.getExchangeId());
            wrapper.like(StrUtil.isNotEmpty(exchangeVoucherEntity.getName()), "name", exchangeVoucherEntity.getName());
            wrapper.eq(StrUtil.isNotEmpty(exchangeVoucherEntity.getNo()), "no", exchangeVoucherEntity.getNo());
            wrapper.eq(exchangeVoucherEntity.getType() != null, "type", exchangeVoucherEntity.getType());
            wrapper.eq(exchangeVoucherEntity.getIsBind() != null, "is_bind", exchangeVoucherEntity.getIsBind());
            wrapper.eq(exchangeVoucherEntity.getStatus() != null, "status", exchangeVoucherEntity.getStatus());
            wrapper.eq(exchangeVoucherEntity.getMemberId() != null, "member_id", exchangeVoucherEntity.getMemberId());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<ExchangeVoucherDto> pageExchangeVoucher(Page<ExchangeVoucherDto> page, ExchangeVoucherDto exchangeVoucherDto) {
        QueryWrapper<ExchangeVoucherEntity> wrapper = getWrapper(exchangeVoucherDto);
        Page<ExchangeVoucherEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<ExchangeVoucherEntity> exchangeVoucherPage = baseMapper.selectPage(pageEntity, wrapper);
        Page<ExchangeVoucherDto> pageDto = new Page(exchangeVoucherPage.getCurrent(), exchangeVoucherPage.getSize(), exchangeVoucherPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(exchangeVoucherPage.getRecords(), ExchangeVoucherDto.class));
        return pageDto;
    }

    @Override
    public IPage<ExchangeVoucherDto> pageExchangeVoucherExtends(Page<ExchangeVoucherDto> page, ExchangeVoucherDto exchangeVoucherDto) {
        Map<String, Long> pageMap = BizCommonUtil.getPageParam(page);
        ExchangeVoucherEntity exchangeVoucherEntity = ConvertUtil.transformObj(exchangeVoucherDto, ExchangeVoucherEntity.class);
        List<ExchangeVoucherEntity> exchangeVoucherList = baseMapper.extendsList(pageMap, exchangeVoucherEntity);
        Long total = baseMapper.extendsCount(exchangeVoucherEntity);
        Page<ExchangeVoucherDto> pageDto = new Page(page.getCurrent(), page.getSize(), total);
        pageDto.setRecords(ConvertUtil.transformObjList(exchangeVoucherList, ExchangeVoucherDto.class));
        return pageDto;
    }

    @Override
    public ExchangeVoucherDto getByIdExtends(Long id) {
        ExchangeVoucherEntity exchangeVoucherEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(exchangeVoucherEntity, ExchangeVoucherDto.class);
    }

    @Override
    public List<ExchangeVoucherDto> listExchangeVoucher(ExchangeVoucherDto exchangeVoucherDto) {
        QueryWrapper<ExchangeVoucherEntity> wrapper = getWrapper(exchangeVoucherDto);
        List<ExchangeVoucherEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list, ExchangeVoucherDto.class);
    }

    @Override
    public void generate(HashMap<String, String> params) {
        String exchangeId = params.get("exchangeId");
        String element = params.get("element");
        String isPrefix = params.get("isPrefix");
        String prefix = params.get("prefix");
        String length = params.get("length");
        String validityPeriod = params.get("validityPeriod");
        ExchangeConfigEntity config = exchangeConfigMapper.selectById(exchangeId);
        ArrayList<ExchangeVoucherEntity> exchangeVoucherEntities = new ArrayList<>();
        Integer num = Integer.parseInt(params.get("num"));
        for (int i = 1; i <= num; i++) {
            ExchangeVoucherEntity temp = new ExchangeVoucherEntity();
            temp.setExchangeId(config.getId());
            temp.setStartTime(config.getStartTime());
            temp.setEndTime(config.getEndTime());
            temp.setName(config.getName());
            temp.setType(config.getType());
            temp.setExpiredTime(DateTime.now().offset(DateField.DAY_OF_YEAR, Integer.parseInt(validityPeriod)));
            if (CodeTypeEnum.NUMBER_CODE.getCode().equals(config.getType())) {
                temp.setNo(getNo(element, length, isPrefix, prefix));
            } else if (CodeTypeEnum.NORMAL_CODE.getCode().equals(config.getType())) {
                temp.setNo(config.getUrl() + "?id=" + config.getId());
            }
            temp.setStatus(VoucherStatusEnum.UNUSED.getCode());
            temp.setIsBind(YesNoEnum.NO.getCode());
            exchangeVoucherEntities.add(temp);
        }
        saveBatch(exchangeVoucherEntities);
    }

    @Override
    public String getNo(String element, String length, String isPrefix, String prefix) {
        String result = "";
        //包含大小写字母的
        result = RandomUtil.randomString(Integer.parseInt(length));
        if (element.replace(",", "").equals("1")) {
            //纯数字
            result = RandomUtil.randomNumbers(Integer.parseInt(length));
        }
        if (isPrefix.equals("true")) {
            result = prefix + result;
        }
        return result;
    }

    @Override
    public List<ExchangeVoucherDto> downloadList(ExchangeVoucherDto exchangeVoucherDto) {
        ExchangeVoucherEntity exchangeVoucherEntity = ConvertUtil.transformObj(exchangeVoucherDto, ExchangeVoucherEntity.class);
        List<ExchangeVoucherEntity> exchangeVoucherList = baseMapper.extendsList(null, exchangeVoucherEntity);
        return ConvertUtil.transformObjList(exchangeVoucherList, ExchangeVoucherDto.class);
    }

}

